{
 "metadata": {
  "name": "",
  "signature": "sha256:dc513d9d5177b259f4e864715a38b2313d192924a7180f568a5420d44db25e7f"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sklearn import datasets\n",
      "import matplotlib.pyplot as plt\n",
      "import random\n",
      "import numpy as np\n",
      "%matplotlib inline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "iris = datasets.load_iris()\n",
      "print (sum(iris.target==1))\n",
      "print (len(iris.target))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "50\n",
        "150\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data_size = sum(iris.target==0) + sum(iris.target==1)\n",
      "index = [i for i in range(data_size)]\n",
      "random.shuffle(index)\n",
      "iris.data = iris.data[index]\n",
      "iris.target = iris.target[index]\n",
      "\n",
      "#split data\n",
      "test_size = 20\n",
      "x_train = iris.data[test_size:]\n",
      "x_test = iris.data[:test_size]\n",
      "y_train = iris.target[test_size:]\n",
      "y_test = iris.target[:test_size]\n",
      "\n",
      "print (x_train.shape)\n",
      "print (y_test.shape)\n",
      "print (x_test)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(80, 4)\n",
        "(20,)\n",
        "[[ 6.   2.7  5.1  1.6]\n",
        " [ 4.6  3.4  1.4  0.3]\n",
        " [ 5.8  2.7  4.1  1. ]\n",
        " [ 4.5  2.3  1.3  0.3]\n",
        " [ 6.   3.4  4.5  1.6]\n",
        " [ 5.7  3.8  1.7  0.3]\n",
        " [ 5.4  3.7  1.5  0.2]\n",
        " [ 6.1  2.8  4.   1.3]\n",
        " [ 4.9  3.1  1.5  0.1]\n",
        " [ 5.7  2.6  3.5  1. ]\n",
        " [ 5.1  2.5  3.   1.1]\n",
        " [ 5.2  2.7  3.9  1.4]\n",
        " [ 5.2  3.4  1.4  0.2]\n",
        " [ 5.4  3.9  1.7  0.4]\n",
        " [ 5.6  3.   4.5  1.5]\n",
        " [ 5.5  2.6  4.4  1.2]\n",
        " [ 5.3  3.7  1.5  0.2]\n",
        " [ 5.7  4.4  1.5  0.4]\n",
        " [ 5.   3.3  1.4  0.2]\n",
        " [ 6.1  2.8  4.7  1.2]]\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def sigmoid(x):\n",
      "    return 1.0/(1.0+np.exp(-x))\n",
      "\n",
      "def cost(xMat, yMat, w):\n",
      "    left = np.multiply(yMat, np.log(sigmoid(xMat*w)))\n",
      "    right = np.multiply(1-yMat, np.log(1-sigmoid(xMat*w)))\n",
      "    return np.sum(left+right)/(-len(xMat))\n",
      "\n",
      "def train(xdata, ylabel, lr = 0.001, epoches = 10000):\n",
      "    xMat = np.mat(xdata)\n",
      "    yMat = np.mat(ylabel).T\n",
      "    \n",
      "    costList = []\n",
      "    m,n = np.shape(xMat)\n",
      "    w = np.mat(np.ones((n,1)))\n",
      "    b = np.mat(1)\n",
      "    for i in range(epoches):\n",
      "        a = sigmoid(xMat*w + b)\n",
      "        w_grad = np.dot(xMat.T, (a-yMat))/m\n",
      "        w -= np.dot(lr,w_grad)\n",
      "        b_grad = np.sum((a-yMat))\n",
      "        b -= np.dot(lr,b_grad)\n",
      "        if i % 50 == 0:\n",
      "            costList.append(cost(xMat, yMat, w))\n",
      "    return w, b, costList\n",
      "    \n",
      "    \n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "w, b, costList = train(x_train, y_train)\n",
      "print (w)\n",
      "print (b)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[[-0.74190877]\n",
        " [-0.80646673]\n",
        " [ 1.6060225 ]\n",
        " [ 1.37912549]]\n",
        "[[1]]\n"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x = np.linspace(0,10000,200)\n",
      "print (x.shape)\n",
      "print (len(costList))\n",
      "plt.plot(x, costList, c='r')\n",
      "plt.title(\"Train\")\n",
      "plt.xlabel('Epochs')\n",
      "plt.ylabel('Cost')\n",
      "plt.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(200,)\n",
        "200\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEWCAYAAABliCz2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAF8pJREFUeJzt3X+wHWd93/H3915d27IsWT/ukWxk\n42vGJtRQsI3K2EDTxm2IIUyYtDQ2BQIE6kDT1hQmCR6mZBIyQ5tQhrgwBEMoEIztlNgJdQ0Oxk4p\nAwhLxPgHtvEPnGDLtq5khH5gy9LVt3/sXuncq/vjSDp79t7d92tm5+w+Z8/ZZ+9Kn33Oc3afE5mJ\nJKn5huqugCRpMAx8SWoJA1+SWsLAl6SWMPAlqSUMfElqCQNfOkoRMRwRuyPiuXXXRepFeB2+2iIi\ndnctngjsBSbK5d/MzKsHXytpcAx8tVJEPAK8IzNvmWOdJZm5f3C1kqpll45Uiog/jIjrIuKaiNgF\nvCkiLoyI70TEjoh4PCKujIiRcv0lEZERMVYuf6F8/isRsSsivh0RZ9a4S9IUBr401a8CXwROBq4D\n9gOXA6PAK4CLgd+c4/X/FvgvwGrgH4APVllZ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88ul38XGMnMP4yIrwK7KYZO+KvM3F3xrkpzsoWvtstZ5o/E3q75\nCQ59N/bLFGOlnA/c3jUypFQLA19td0nX47fL+W9RjN4J8Ebg/5XzXwfeBQd/h/fk2d40IoaA0zPz\nNuB3KYb4PexThjRItjjUBksj4o6u5a9m5uSlmasi4k6KVvobyrL/SPFrVL9N8ctUbyvLLweuioi3\nU7Tk30UxMuJMhoEvlCeFAK7MzB192yPpKNiHr9Yq+/A3ZOa2uusiDYJdOpLUErbwJaklbOFLUksY\n+JLUEga+JLWEgS9JLWHgS1JL/H8f3p7QvQY4eQAAAABJRU5ErkJggg==\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7fcc7f717c88>"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def predict(x_data, w):\n",
      "    xMat = np.mat(x_data)\n",
      "    w = np.mat(w)\n",
      "    print (xMat.shape)\n",
      "    print (w.shape)\n",
      "    print ((xMat*w).shape)\n",
      "    return [1 if x> 0.5 else 0 for x in sigmoid(xMat*w)]\n",
      "\n",
      "predictions = predict(x_train, w)\n",
      "print (np.sum(predictions==y_train)/len(y_train))\n",
      "predictions = predict(x_test, w)\n",
      "print (np.sum(predictions==y_test)/len(y_test))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(80, 4)\n",
        "(4, 1)\n",
        "(80, 1)\n",
        "1.0\n",
        "(20, 4)\n",
        "(4, 1)\n",
        "(20, 1)\n",
        "1.0\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}